import router from './router'
import {ajax,api} from '../http'

//全局的路由前置守卫
function beforeEach(){
    router.beforeEach((to,from,next)=>{
        // console.log(from)
        // console.log(to)
        if(to.meta.title){
            document.title = to.meta.title
        }else{
            document.title = '电商后台系统'
        }
    
        //判断当前是否登录
        let token = localStorage.token
        if(token){
            ajax({
                path: api.User.yz
            }).then(res=>{
                console.log(res);
                if(res.errcode && res.errcode == 1){
                    if(to.path === '/login'){
                        next()
                    }else{
                        next('/login')
                    }
                }else{
                    next()
                }
            })
        }else{
            if(to.path === '/login'){
                next()
            }else{
                next('/login')
            }
        }
        
    })
}

//全局后置守卫
function afterEach(){
    router.afterEach((to,from)=>{

    })
}

export {
    beforeEach,
    afterEach
}